/**   
* @Title: RedisServiceImpl.java 
* @Package com.xzy.service.impl 
*/ 
package cn.circle.redis;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

import cn.circle.system.util.GsonUtil;



/**
 * @ClassName: RedisServiceImpl
 * @Description: TODO
 * @author hong
 * @date 2017-8-5 下午4:09:08
 *
 */
public class RedisServiceImpl implements IRedisService{
//	@Autowired  
    private RedisTemplate<String, ?> redisTemplate;  
      
    public boolean set(final String key, final String value, final long liveTime) {  
        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {  
            public Boolean doInRedis(RedisConnection connection) throws DataAccessException {  
                RedisSerializer<String> serializer = redisTemplate.getStringSerializer();  
                connection.set(serializer.serialize(key), serializer.serialize(value));
                if(liveTime > 0){
                	connection.expire(serializer.serialize(key), liveTime);
                }
                return true;  
            }  
        });
        return result;  
    }  
    
    public boolean set(final String key, final String value) {  
    	 return set(key, value, 0);
    }  
    
    public boolean set(final String key, final Object object, final long liveTime) {  
        String value = GsonUtil.GsonString(object);
        return set(key, value, liveTime);
    } 
    
    public boolean set(final String key, final Object object) {  
        String value = GsonUtil.GsonString(object);
        return set(key, value, 0);
    }
  
    public <T> T get(final String key, Class<T> clz) {  
        String result = get(key);
        if(GsonUtil.isGoodJson(result)){
        	return GsonUtil.fromJson(result, clz);
        }
        return null;    
    }
    
    public String get(final String key){  
        String result = redisTemplate.execute(new RedisCallback<String>() {  
            public String doInRedis(RedisConnection connection) throws DataAccessException {  
                RedisSerializer<String> serializer = redisTemplate.getStringSerializer();  
                byte[] value =  connection.get(serializer.serialize(key));  
                return serializer.deserialize(value);  
            }  
        });  
        return result;  
    }  
    
    public long decr(final String key) {  
    	return decr(key, 1);
    }  
    
    public long decr(final String key, final long step) {  
        long result = redisTemplate.execute(new RedisCallback<Long>() {  
            public Long doInRedis(RedisConnection connection) throws DataAccessException { 
            	RedisSerializer<String> serializer = redisTemplate.getStringSerializer();  
                long count = connection.decrBy(serializer.serialize(key), step);
                return count;
            }  
        });
        return result;  
    }
    
    public boolean hasKey(final String key) {  
        Boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {  
            public Boolean doInRedis(RedisConnection connection) throws DataAccessException { 
            	RedisSerializer<String> serializer = redisTemplate.getStringSerializer();  
                return connection.exists(serializer.serialize(key));
            }  
        });
        return result;  
    }
    
    public long incr(final String key) {  
    	return incr(key, 1);
    }  
    
    public long incr(final String key, final long step) {  
        long result = redisTemplate.execute(new RedisCallback<Long>() {  
            public Long doInRedis(RedisConnection connection) throws DataAccessException { 
            	RedisSerializer<String> serializer = redisTemplate.getStringSerializer();  
                long count = connection.incrBy(serializer.serialize(key), step);
                return count;
            }  
        });
        return result;  
    } 
    
    public boolean expire(final String key, long expire) {  
        return redisTemplate.expire(key, expire, TimeUnit.SECONDS);  
    }  
  
    /*public <T> boolean setList(String key, List<T> list) {  
        String value = JSONUtil.toJSONString(list); 
        return set(key,value);  
    }
    
    public <T> List<T> getList(String key,Class<T> clz) {  
        String json = get(key);  
        if(json!=null){  
            List<T> list = JSONUtil.toList(json, clz);  
            return list;  
        }  
        return null;  
    }  
    
    public long lpush(final String key, Object obj) {  
        final String value = JSONUtil.toJSONString(obj);  
        return lpush(key, value);
    } 
    
    */ 
    
    public <T> boolean setList(String key, List<T> list) {  
        String value = GsonUtil.GsonString(list);
        return set(key,value);  
    }
    
    public <T> List<T> getList(String key,Class<T> clz) {  
        String json = get(key);  
        if(json!=null){  
            List<T> list = GsonUtil.GsonToList(json, clz);
            return list;  
        }  
        return null;  
    }
  
    public long lpush(final String key, Object obj) {  
        final String value = GsonUtil.GsonString(obj);
        return lpush(key, value);
    }  
    
    public long lpush(final String key, final String value) { 
        long result = redisTemplate.execute(new RedisCallback<Long>() {  
            public Long doInRedis(RedisConnection connection) throws DataAccessException {  
                RedisSerializer<String> serializer = redisTemplate.getStringSerializer();  
                long count = connection.lPush(serializer.serialize(key), serializer.serialize(value));  
                return count;  
            }  
        });  
        return result;  
    }
    
    /*
    public long rpush(final String key, Object obj) {  
        final String value = JSONUtil.toJSONString(obj);  
        return rpush(key, value);
    }*/
    
    public long rpush(final String key, Object obj) {  
    	final String value = GsonUtil.GsonString(obj);
        return rpush(key, value);
    }
    
    public long rpush(final String key, final String value) {  
        long result = redisTemplate.execute(new RedisCallback<Long>() {  
            public Long doInRedis(RedisConnection connection) throws DataAccessException {  
                RedisSerializer<String> serializer = redisTemplate.getStringSerializer();  
                long count = connection.rPush(serializer.serialize(key), serializer.serialize(value));  
                return count;  
            }  
        });  
        return result;  
    }
    
    public long lset(final String key, final String value, final long index) {  
        long result = redisTemplate.execute(new RedisCallback<Long>() {  
            public Long doInRedis(RedisConnection connection) throws DataAccessException {  
                RedisSerializer<String> serializer = redisTemplate.getStringSerializer();  
                connection.lSet(serializer.serialize(key), index, serializer.serialize(value));
                return index;
            }  
        });  
        return result;  
    }
    
    public String lIndex(final String key, final long index) {  
        String result = redisTemplate.execute(new RedisCallback<String>() {  
            public String doInRedis(RedisConnection connection) throws DataAccessException {  
                RedisSerializer<String> serializer = redisTemplate.getStringSerializer();  
                byte[] res =  connection.lIndex(serializer.serialize(key), index);  
                return serializer.deserialize(res);  
            }  
        });  
        return result;  
    }
    
    public String lpop(final String key) {  
        String result = redisTemplate.execute(new RedisCallback<String>() {  
            public String doInRedis(RedisConnection connection) throws DataAccessException {  
                RedisSerializer<String> serializer = redisTemplate.getStringSerializer();  
                byte[] res =  connection.lPop(serializer.serialize(key));  
                return serializer.deserialize(res);  
            }  
        });  
        return result;  
    }
    
    public <T> T lpop(final String key, Class<T> clz) {  
        String result = lpop(key);
        if(GsonUtil.isGoodJson(result)){
        	return GsonUtil.fromJson(result, clz);
        }
        return null;  
    }
    
    public String rpop(final String key) {  
        String result = redisTemplate.execute(new RedisCallback<String>() {  
            public String doInRedis(RedisConnection connection) throws DataAccessException {  
                RedisSerializer<String> serializer = redisTemplate.getStringSerializer();  
                byte[] res =  connection.rPop(serializer.serialize(key));  
                return serializer.deserialize(res);  
            }  
        });  
        return result;  
    }
    
    public <T> T rpop(final String key, Class<T> clz) {  
        String result = rpop(key);
        if(GsonUtil.isGoodJson(result)){
        	return GsonUtil.fromJson(result, clz);
        }
        return null;    
    }

	public RedisTemplate<String, ?> getRedisTemplate() {
		return redisTemplate;
	}

	public void setRedisTemplate(RedisTemplate<String, ?> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	/**
	 * 删除key
	 * 
	 * @param key
	 * @param value
	 */
	public void delete(final String key) {

		redisTemplate.execute(new RedisCallback<Object>() {
			public Long doInRedis(RedisConnection connection) throws DataAccessException {
				connection.del(key.getBytes());
				return 1L;
			}
		});
	
		
	}

}
